from django.db import models
import os

class SmmWebring(models.Model):
    
    def parseWebring(branche, xmlpath, ttlhours, style, debug):
        self.branche = branche
        self.templateurl = templateurl
        self.ttlhours = ttlhours
        self.style = style
        self.debug = debug
        self.cachedWebring = dirname(__FILE__).'/cache/webring.html'
    
        self.xmlurl = xmlpath."/smmwebring.xml";
        self.templateurl = sprintf("%s/styles/%s/template.xsl", xmlpath, style);
        self.cssurl = sprintf("%s/styles/%s/style.css", xmlpath, style);
        
        if self.debug:
            print "\n<!-- START Webring -->\n"
        endif
            
        if os.path.exists(self.cachedWebring):
            if filetime(self.cachedWebring) < time() - (self.ttlhours*3600):
                if self.debug: print("<!-- cache file excist but outdated, from live -->\n");
                    webringHTML = self.getWebringFromLive();
                    self.saveWebringToCache(webringHTML);
            else:
                    if self.debug: print("<!-- cache file excist, from cache -->\n");
                        webringHTML = self.getWebringFromCache();
                    endif
                    if !webringHTML: // Cache file is empty
                        if self.debug: print("<!-- cache file is empty, create new cache file, from live -->\n");
                            webringHTML = self.getWebringFromLive();
                            self.saveWebringToCache(webringHTML);
                        endif
                    endif
                endif
            endif
            
        else:
            if self.debug: print("<!-- cache file doesn't excist, create new cache file, from live -->\n");
                webringHTML = self.getWebringFromLive();
                self.saveWebringToCache(webringHTML);
            endif
        endif
        if self.debug print("<!-- parseStylesheet, from live -->\n");
            print(self.parseStylesheet());
        if self.debug print("<!-- parseWebring -->\n");
            print(webringHTML);
        if self.debug print("<!-- END Webring -->\n");